﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.WPF.Command;
using OpenExpressApp.MetaModel;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.Editors;
using OpenExpressApp.Module.WPF.Editors;
using Csla.Core;
using OpenExpressApp.Types;
using OpenExpressApp.Module.Command;
using OpenExpressApp.WPF;
using OpenExpressApp.Template;
using System.Collections;
using System.ComponentModel;

namespace OpenExpressApp.Module.WPF.Command
{
    [Command(CommandNames.ClearLookupPropertyValue, ToolbarType = ToolbarType.Manual, Label = "清除属性值", ToolTip = "清除属性值")]
    public class ClearLookupPropertyValueCommand : PropertyEditorCommand
    {
        public override void Execute(PropertyEditor editor)
        {
            //清空Object
            string propertyName = editor.PropertyInfo.LookupAttribute.LookupPropertyName;
            if (string.IsNullOrEmpty(propertyName) == false)
            {
                var entity = editor.View.CurrentObject;
                if (entity != null)
                {
                    entity.SetPropertyValue(propertyName, null);
                }
            }

            //清空key
            editor.PropertyValue = null;
        }
    }

    [Command(CommandNames.LookupSelectAdd, ImageName = "LookupSelectAdd.bmp", ToolbarType = ToolbarType.Manual, Label = "选择新增", ToolTip = "选择新增")]
    public class LookupSelectAddCommand : WPFListViewCommand, ISelectAddCommandParams
    {
        public override bool CanExecute(ListObjectView view)
        {
            return null != view.Data;
        }

        public override void Execute(ListObjectView view)
        {
            var form = OpenApplication.CreatePopupForm(BoType);
            bool? result = OpenApplication.ShowDialog(form, new DialogOptions()    //弹出弹出窗体
            {
                AllowResize = true,
                DialogTitle = "选择"+ApplicationModel.GetBusinessObjectInfo(BoType).Label,
                DialogSize = new System.Windows.Size(800, 600)
            },
                null, new string[] { CommandNames.SelectAll }
            );

            if (result == true)
            {
                ObjectView selectView = (form as IFrameTemplate).View;
                var activeObjects = selectView.GetActiveObjects();
                for (int i = activeObjects.Count - 1; i >= 0; i--)
                {
                    BusinessBase src = activeObjects[i] as BusinessBase;
                    //如果已经存在，则退出
                    bool eixst = false;
                    foreach (var item in view.Data as IList)
                    {
                        if (item.GetPropertyValue(BoType.Name).GetPropertyValue(DBConvention.FieldName_Id).Equals(src.GetPropertyValue(DBConvention.FieldName_Id)))
                        {
                            eixst = true;
                            break;
                        }
                    }
                    if (eixst) continue;
                    //新增
                    object newObject = (view.Data as IBindingList).AddNew();
                    //设置选择对象对应的属性值
                    newObject.SetPropertyValue(BoType.Name, src);
                    newObject.SetPropertyValue(BoType.Name + DBConvention.FieldName_Id, src.GetPropertyValue(DBConvention.FieldName_Id));

                    //设置父属性的值
                    newObject.SetPropertyValue(view.GetKeyToParentView(), view.Parent.CurrentID);
                    newObject.SetPropertyValue(view.GetKeyObjToParentView(), view.Parent.CurrentObject);
                }
            }
        }

        #region ISelectAddCommandParams Members

        public Type BoType {get ;set;}

        #endregion
    }
}
